package com.seek.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.seek.commonutils.JwtUtils;
import com.seek.commonutils.ResponseResult;
import com.seek.eduorder.client.CourseClient;
import com.seek.eduorder.entity.Order;
import com.seek.eduorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author wml
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/eduorder/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private CourseClient courseClient;

    // 生成订单的方法
    @PostMapping("createOrder/{courseId}")
    public ResponseResult createOrder(@PathVariable String courseId, HttpServletRequest request) {
        // 创建订单，返回订单号（不是订单 id，是订单编号）
        String orderNo = orderService.createOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return ResponseResult.ok().data("orderNo", orderNo);
    }

    // 根据订单编号查询订单信息
    @GetMapping("getOrderInfo/{orderNo}")
    public ResponseResult getOrderInfo(@PathVariable String orderNo) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        Order order = orderService.getOne(queryWrapper);
        return ResponseResult.ok().data("order", order);
    }

    // 根据课程 id 和用户 id 查询订单表中的订单状态
    @GetMapping("isBuyCourse/{courseId}")
    public boolean isBuyCourse(@PathVariable String courseId, @RequestParam String memberId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        queryWrapper.eq("member_id", memberId);
        queryWrapper.eq("status", 1);
        int count = orderService.count(queryWrapper);
        if (count == 1) {
            return true;
        }
        return false;
    }

    // 根据订单编号修改订单状态
    @PutMapping("updateOrderStatusByOrderNo/{orderNo}/{status}/{courseId}")
    public ResponseResult updateOrderStatusByOrderNo(@PathVariable String orderNo, @PathVariable String status,
                                                     @PathVariable String courseId) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_no", orderNo);
        updateWrapper.set("status", status);
        orderService.update(updateWrapper);
        courseClient.updateCourseInfoOrder(courseId);
        return ResponseResult.ok();
    }

    // 根据用户 id 分页查询订单
    @GetMapping("getOrderListByMemberId/{memberId}/{current}/{size}")
    public ResponseResult getOrderListByMemberId(@PathVariable String memberId, @PathVariable int current,
                                                 @PathVariable int size) {
        int start = (current - 1) * size;
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        int count = orderService.count(queryWrapper);
        queryWrapper.orderByDesc("gmt_create");
        queryWrapper.last("limit " + start + "," + size);
        List<Order> orderList = orderService.list(queryWrapper);
        return ResponseResult.ok().data("orderList", orderList).data("count", count);
    }

}
